﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using ZLCMS.Business;
using System.Collections;
using ZLCMS.Data.Model;
using ZLCMS.Business.Config;
using ZLCMS.Business.DwzFrame;
using System.Reflection;
using ZLCMS.Data.Common;
using ZLCMS.Business.Util;
using Newtonsoft.Json;
using ZLCMS.Business.UploadFileInfo;
using NHibernate;

namespace ZLCMS.Web.SystemManage.Basics.Doc
{
    public partial class Add : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        protected CmsKindInfo _kindDB;

        protected CmsDocConfig _config;

        protected void Page_Load(object sender, EventArgs e)
        {
            //初始化DWZ控件
            _framePage = new FrameHelper(this);

            //初始化全局变量值
            InitializeGlobalValue();

            //初始化控件值
            InitializeContent();

            if (IsPostBack)
            {
                SaveDoc();
            }
        }

        protected void InitializeGlobalValue()
        {
            doc_kind.Value = (String.IsNullOrEmpty(Request["Kind"]) ? "Default" : Request["Kind"]);
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                try
                {
                    _kindDB = session.Get<CmsKindInfo>(doc_kind.Value);
                    try
                    {
                        UploadConfig cfg = JsonConvert.DeserializeObject<UploadConfig>(_kindDB.ExtendJasonContent);
                        fc_fileurl.Config = cfg;
                        fc_imgurl.Config = cfg;
                    }
                    catch { }
                    _config = session.Get<CmsDocConfig>(_kindDB.ConfigId);
                }
                catch (System.Exception ex)
                {
                    _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                    return;
                }
            }

            fc_fileurl.FileType = _config.FileType;
        }

        protected void InitializeContent()
        {
            ddTreeControl.Kind = doc_kind.Value;

            //页面控件值
            if (!IsPostBack)
            {
                tb_cdate.Text = DateTime.Now.ToString("yyyy-MM-dd");
            }
        }

        /// <summary>
        /// 保存数据库
        /// </summary>
        protected void SaveDoc()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ITransaction tran = session.BeginTransaction();
                try
                {

                    CmsDocInfo doc = new CmsDocInfo();

                    doc.TypeId = ddTreeControl.SelectedNodeId;

                    doc.Title = txtTitle.Text;
                    doc.Name = txtName.Text;
                    doc.Author = txtWriter.Text;

                    doc.Summary = txtSummary.Value;
                    doc.Content = txtContent.Value;

                    doc.Url = txtUrl.Text;
                    doc.ThumbnailUrl = fc_imgurl.Value;
                    doc.AttachmentUrl = fc_fileurl.Value;
                    doc.IsHot = DatabaseHelper.ConvertBool(chkHot.Checked);
                    doc.IsRecommend = DatabaseHelper.ConvertBool(chkRcmd.Checked);
                    doc.ShowOrder = int.Parse(tb_order.Text);

                    doc.WebTitle = txtWebTitle.Text;
                    doc.WebKeyword = txtKeyword.Text;
                    doc.WebDesc = txtDesc.Text;

                    doc.CreateDate = DateTime.Parse(tb_cdate.Text + " 00:00:00");
                    doc.UpdateDate = DateTime.Now;

                    doc.Kind = doc_kind.Value;
                    session.Save(doc);

                    DocHtml htm = new DocHtml();

                    htm.DocId = doc.Id;
                    htm.IsDone = DatabaseHelper.ConvertBool(false);

                    session.Save(htm);

                    session.Flush();
                    tran.Commit();
                }
                catch (System.Exception ex)
                {
                    tran.Rollback();
                    _framePage.Alert(new ReturnCode() { statusCode = 300, message = "操作出错-" + ex.Message });
                }
            }

            _framePage.Alert(new ReturnCode()
            {
                statusCode = 200,
                message = "操作成功!"//,
                //callbackType = "closeCurrent"//,
                //navTabId = _framePage.NavTabId
            });
        }
    }
}
